package com.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import modle.Card;
import modle.CardGame;
import modle.CardGames;
import modle.CardType;
import modle.User;
import util.CardPlay;
import util.JsonChange;

/**
 * Servlet implementation class PlayGameServlet
 */
@WebServlet("/CardJudgementServlet")
/**
 * 用于判断用户出牌是否正确，和是否可以选择不出
 * @author WXE
 *
 */
public class CardJudgementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CardJudgementServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		InputStreamReader reader=new InputStreamReader(request.getInputStream(),"UTF-8");
//		System.err.println(id);
		String[] strs = request.getParameterValues("list");
		
		List<Card> list = JsonChange.toList(strs);
		String result;
		int f;
		User user = (User) request.getSession().getAttribute("user");
		CardGames cardGames = (CardGames) this.getServletContext().getAttribute("CardGames");
		
		System.out.println(user.toString());
		int cardGameID = user.getCardGameID();
		Map<Integer, CardGame> games = cardGames.getCardGames();
		CardGame cardGame = games.get(cardGameID);
		System.out.println("cardGame"+cardGame.getId());
		//点击不出
		int role0 = 0;
		int role1 = 1;
		int role2 = 2;
		if( list==null ) { 
			cardGame.getFlag()[role1] = false;
			cardGame.getCurrentList()[role1]=list;
			if( cardGame.getFlag()[role0] == false && cardGame.getFlag()[role2] == false ) {
				//必须出牌不能不出
				result = "0";
			}else {
				result = "1";
			}
			
		}else {
			//玩家主动出牌
			if( cardGame.getFlag()[role0] == false && cardGame.getFlag()[role2] == false ) { 
				//System.out.println(cardGame.getId()+"主动出牌");
				CardType re = CardPlay.jugdeType(list);
				//System.out.println(cardGame.getId()+"我的牌型" + re);
				if( re == CardType.c0 ) {
					f = 0;
				}else {
					f = 1;
				}
			}else {
				//System.out.println(cardGame.getId()+"跟牌");
				f = CardPlay.checkCards(list, cardGame.getCurrentList());
			}
			if( f == 1 ) {
				result = "1";
				cardGame.getFlag()[role1] = true;
				cardGame.getCurrentList()[role1] = list;
			}else {
				cardGame.getFlag()[role1] = false;
				result = "0";
			}
		}
        response.getWriter().println(result);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
